<?php

/**
 * copy_contents actions.
 *
 * @package    master_directory
 * @subpackage copy_contents
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class copy_contentsActions extends sfActions
{
	protected $inserted_contents = 0;
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  		$posible_directories = Doctrine_Core::getTable ( 'MasterDirectory' )->retreiveActiveDirectories ();
  		
  		$directories_with_content = array();
  		
  		$directories_without_content = array();
  		
  		foreach ($posible_directories as $directory){
  			$categories = $directory->getCategory();
  			$categories_with_content = array();
  			$categories_without_content = array();
  			foreach ($categories as $category){
  				
  				$category_contents = $category->getCategoryContent();
  			//echo $directory->getName() . ' - ' .  $category->getName() .' - ' .count($category_contents) . '<br>';
  				if (count($category_contents) > 0){
  					array_push($categories_with_content, $category);
  				}
  				else{
  					array_push($categories_without_content, $category);
  				}
  			}
  			if (count($categories_with_content) > 0){

  				$directory_category_content = array();
  				array_push($directory_category_content, $directory->getName());
  				array_push($directory_category_content, $categories_with_content);
  				$directories_with_content[$directory->getId()] = $directory_category_content;
  			}
  			if (count($categories_without_content) > 0){
  				
				$directory_category_content_empty = array();
  				array_push($directory_category_content_empty, $directory->getName());
  				array_push($directory_category_content_empty, $categories_without_content);
  				$directories_without_content[$directory->getId()] = $directory_category_content_empty;  				
  			}
  		}
  		
  		$this->directories_with_content = $directories_with_content;
  		
  		$this->getUser()->setAttribute('directories_with_content', $directories_with_content);
  		
  		$this->getUser()->setAttribute('directories_without_content', $directories_without_content);
  }
  
  public function executeGetCategories(sfWebRequest $request){
  	$directory_id = $request->getParameter('directory_id');
  	$directories_with_content = $this->getUser()->getAttribute('directories_with_content');
  	
  	$directories_name_categories =  $directories_with_content[$directory_id];
  	
  	$this->categories = $directories_name_categories[1];
  	
  }
  
  public function executeGetMatchingCategories(sfWebRequest $request){
  	
		
  	
  	
  	$category_id = $request->getParameter('category_id');
  	
  	$directories_without_content = $this->getUser()->getAttribute('directories_without_content');
  	
 	
  	$matching_categories = array();
  	
  	$root_category1 = Doctrine_Core::getTable ( 'Level' )->getHydratedRoot($category_id);
  	
  	//echo $root_category1;
  	
  	foreach($directories_without_content as $directory_id => $directory_name_categories){
  		$categories = $directory_name_categories[1];
  		
  
  		foreach ($categories as $category){
  			//echo $category->getName() . '<br>';
  			$root_category2 = Doctrine_Core::getTable ( 'Level' )->getHydratedRoot($category->getId());
  			
  			//echo $root_category1->getId() . ' and ' . $root_category2->getId() . '<br>';
			if($root_category2 != null){
  			$comparison = Doctrine_Core::getTable ( 'Level' )->compareTree($root_category1, $root_category2, true );
  			if ($comparison){
  				array_push($matching_categories, $category);
  			}
}
  		}
  	}
  	$this->matching_categories = $matching_categories;
  	
  }  
  
  public function executeCopyContents(sfWebRequest $request){
  	$category_with_contents = $request->getParameter('sl_categories');
  	$categories_without_contents = $request->getParameter('empty_category');
  	
  	$root_contents = Doctrine_Core::getTable('CategoryContent')->getCategoryContentRootsByCategoryId($category_with_contents);
  	
  	foreach ($categories_without_contents as $category_without_contents){
  		foreach ($root_contents as $root_content){
  			$this->insertContent($root_content, null, $category_without_contents);
  		}
  	}
  	
 
  	
  	$message = $this->inserted_contents . " contents were successfully copied";
  	
  	$this->getUser()->setFlash('saved_contents', $message, true);
  	
  	return $this->redirect("copy_contents/index");
  	
  	
  }
  
  protected function insertContent($content, $parent, $category_id){
    	$category_content = new CategoryContent();
    	$category_content->setCategoryId($category_id);
    	$category_content->setDescription($content->getDescription());
    	$category_content->setName($content->getName());
    	if ($parent != null){
    		$category_content->setParentId($parent->getId());
    	}
    	
    	$category_content->save();
    	$this->inserted_contents++;
    	
    	$children = $content->getCategoryContent();
    	
    	foreach ($children as $child){
    		$this->insertContent($child, $category_content, $category_id);
    	}
    	
  }
  
}
